package com.lzy.cloud.order.controller;

import com.lzy.cloud.order.dtos.OrderAddressDto;
import com.lzy.cloud.order.request.OrderAddressRequest;
import com.lzy.cloud.order.response.OrderAddressResponse;
import com.lzy.cloud.order.service.OrderAddressService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description:订单收货地址
 * @author: liyun
 * @create: 2018-07-05 13:49
 */
@RestController
@RequestMapping("/order/address")
public class OrderAddressController {
    @Autowired
    private OrderAddressService orderAddressService;

    /**
     * 根据订单id查订单收货地址
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/find/by/order/id/{orderId}",method = RequestMethod.GET)
    public Result queryListByOrderId(@PathVariable("orderId") String orderId) {
        Ensure.that(orderId).isNotNull("00010003");
        OrderAddressDto orderAddressDto = orderAddressService.queryListByOrderId(orderId);
        return new Result(DozerUtils.map(orderAddressDto, OrderAddressResponse.class));
    }

    /**
     * 新增订单收货地址
     * @param request
     * @return
     */
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public Result create(@RequestBody OrderAddressRequest request) {
        Ensure.that(request).isNotNull("00000002");
        Long orderAddressId = orderAddressService.create(DozerUtils.map(request, OrderAddressDto.class));
        Ensure.that(orderAddressId).isGt(0, "00020002");
        return new Result(orderAddressId);
    }

    /**
     * 修改订单收货地址
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody OrderAddressRequest request) {
        Ensure.that(request).isNotNull("00000002");
        boolean result = orderAddressService.update(DozerUtils.map(request, OrderAddressDto.class));
        Ensure.that(result).isTrue("00020005");
        return new Result(result);
    }
}
